<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改会员资料</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- bootstrap-validate -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css" />

    <!-- mui -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- city -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet/less" href="./assets/css/reset.less" />

    <!-- 公共样式 -->
    <link rel="stylesheet/less" href="./assets/css/common.less" />

    <!-- 页面独立的 -->
    <link rel="stylesheet/less" href="./assets/css/UserProfile.less" />
</head>
<body>
    <!-- 用户信息 -->
    <div class="user">
        <div class="avatar wow fadeInUp" onclick="avatar.click()">
            <img src="./assets/images/avatar.png" />
        </div>
    </div>

    <form id="profile" class="profile wow fadeInUp" data-wow-delay="200ms">
        <div class="form-group">
            <label for="mobile">手机号</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="请输入手机号" />
        </div>

        <div class="form-group">
            <label for="nickname">姓名</label>
            <input type="text" class="form-control" name="nickname" id="nickname" placeholder="请输入姓名" />
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <input type="password" class="form-control" id="password" placeholder="密码为空即不修改" />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select id="gender" class="form-control">
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <!-- 文件上传框 -->
        <input type="file" name="avatar" id="avatar" style="display: none;">
        
        <div class="form-group" style="position: relative;">
            <label for="region">地区</label>
            <input type="text" class="form-control" readonly data-toggle="city-picker" name="region" id="region" placeholder="请选择地区" />
        </div>

        <div class="form-group">
            <label for="address">地址</label>
            <input type="text" class="form-control" name="address" id="address" placeholder="请输入地址" />
        </div>

        <div class="action">
            <button class="btn">提交</button>
        </div>
    </form>
</body>
</html>
<!-- 解析less的文件 -->
<script src="./assets/js/less.min.js"></script>

<!-- jquery  -->
<script src="./assets/js/jquery.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap-validate -->
<script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>

<!-- city -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>

<!-- mui -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios -->
<script src="./assets/plugins/axios/axios.min.js"></script>

<!-- 服务请求 -->
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- helpers -->
<script src="./assets/plugins/helpers.js"></script>

<script>
    //实例化wow.js
    new WOW().init();

    // 定义存放登录信息的变量
    let business = {};

    (async () => {
        let result = await check(true,true);

        // 获取验证成功后的数据并且赋值给变量
        business = result.data ? result.data : {};

        if(Object.getOwnPropertyNames(business).length > 0)
        {
            $('.avatar img').attr('src',business.avatar);
            $('#mobile').val(business.mobile);
            $('#nickname').val(business.nickname);
            $('#gender').val(business.gender);
            $('#address').val(business.address);

            $('#region').citypicker('reset');
            $('#region').citypicker('destroy');

            $('#region').citypicker({
                province: business.province,
                city: business.city,
                district: business.district
            });
        }
    })();

    // 实例化表单验证器
    $('#profile').bootstrapValidator({
        // 填写配置项
        message:'表单内容不能为空',// 所有字段默认错误信息
        // 字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields:{
            mobile:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '手机号不能为空'
                    },
                    // 校验字段长度
                    stringLength: {
                        min: 11,// 最小长度
                        max: 11,// 最大长度
                        message: '手机号必须是11位'
                    },
                    // 正则校验
                    regexp: {
                        regexp: /^1[3-9]\d{9}$/, 
                        message:'手机号格式错误'
                    }
                }
            },
            nickname:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '姓名不能为空'
                    }
                }
            },
            address:{
                validators:{
                    // 必填
                    notEmpty:{
                        message: '地址不能为空'
                    }
                }
            }
        }
    }).on('success.form.bv',function(e){
        // 阻止多次提交
        e.preventDefault();
    });

    $('#profile').on('submit',async function(){
        // 判断表单验证是否失败
        if(!$('#profile').data('bootstrapValidator').isValid())
        {
            // 重置表单内容，包括错误信息
            // $('#profile').data('bootstrapValidator').resetForm();

            // 验证没有通过就不执行后面代码
            return false;
        }

        // 获取相应数据并且组装
        let params = {
            id:business.id,
            nickname:$.trim($('#nickname').val()),
            mobile:$.trim($('#mobile').val()),
            gender:$.trim($('#gender').val()),
            address:$.trim($('#address').val())
        }

        // 密码处理
        let password = $.trim($('#password').val());

        if(password)
        {
            params.password = password;
        }

        // 头像处理
        let avatar = $('#avatar')[0].files[0];

        if(avatar)
        {
            params.avatar = avatar;
        }

        // 地区处理
        let citypicker = $('#region').data("citypicker");
        let region = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province");
    
        if(region)
        {
            params.region = region;
        }

        // 发起请求
        let result = await UPLOAD({
            url:'business/profile',
            params
        });

        if(result.code === 1)
        {
            mui.toast(result.msg,{duration:1500});

            // 更新登录信息
            cookie.set('business',JSON.stringify(result.data));

            setTimeout(() => location.href = 'UserIndex.html',1500);

            return false;
        }else{
            mui.toast(result.msg,{duration:1500});

            return false;
        }
        
    
    })

    // 头像预览 给文件上传框绑定一个值更改了会触发的事件
    $('#avatar').change(function(){
        // 获取当前上传文件
        let file = $(this)[0].files[0];

        if(file)
        {
            let Reader = new FileReader();

            Reader.readAsDataURL(file);

            Reader.onload = function()
            {
                $('.avatar img').attr('src',Reader.result);
            }
        }
    })

</script>